//
//  SupecoalAUpervisorMainVC.m
//  Supervisor
//
//  Created by 小宇 on 2017/5/21.
//  Copyright © 2017年 北京怀教网络科技股份有限公司. All rights reserved.
//

#import "SupecoalAUpervisorMainVC.h"
#import "SupecialSupervisorViewController.h"
#import "CommentTableViewCell.h"
#import "SelectViewController.h"
#import "MJRefreshHeader.h"
#import "QuotedSupervisorGC.h"

@interface SupecoalAUpervisorMainVC ()
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray * SupecoalAUpervisorList;
@property (nonatomic,strong)NSMutableDictionary *parameterDic;
@property (nonatomic,assign)NSInteger pageNo;
@end

@implementation SupecoalAUpervisorMainVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadDataRefresh];
    [self HeaderAndFooterRefresh];
}

- (NSString *)setNavTitle{
    return @"专项督导";
}


- (UITableView *)set_tableViewWithFrame{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, Main_Screen_Width,Main_View_Height)];
    self.tableView = tableView;
    return self.tableView;
}

- (void)HeaderAndFooterRefresh{
    LZYWeakSelf(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself loadDataRefresh];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakself loadMoreData];
    }];
}

- (void)loadDataRefresh{
    self.pageNo = 1;
    self.parameterDic = [[NSMutableDictionary alloc]initWithCapacity:3];
    [self.parameterDic setObject:[[User shareInstance] getUserAccessToken] forKey:@"accessToken"];
    [self.parameterDic setObject:@(self.pageNo) forKey:@"pageNo"];
    [self.parameterDic setObject:@(10) forKey:@"pageSize"];

    
    [LZYNetworkHelper GET:dudaoresultlist parameters:self.parameterDic success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        self.SupecoalAUpervisorList = [NSMutableArray arrayWithArray:[QuotedSupervisorGC arrayOfModelsFromDictionaries:responseObject error:nil]];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    } showHub:YES];
}

- (void)loadMoreData{
    LZYWeakSelf(self)
    self.pageNo ++;
    [self.parameterDic setObject:[[User shareInstance] getUserAccessToken] forKey:@"accessToken"];
    [self.parameterDic setObject:@(self.pageNo) forKey:@"pageNo"];
    [self.parameterDic setObject:@(10) forKey:@"pageSize"];
    [self.parameterDic setObject:@"" forKey:@"themeId"];
    [LZYNetworkHelper GET:dudaoresultlist parameters:self.parameterDic success:^(id responseObject) {
        if ([responseObject count] < 10 && [responseObject count] >0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            self.netWorkMoreBlock(responseObject);
        }else if([responseObject count] == 10){
            [self.tableView.mj_footer endRefreshing];
            [weakself.SupecoalAUpervisorList addObjectsFromArray:[QuotedSupervisorGC arrayOfModelsFromDictionaries:responseObject error:nil]];
            [weakself.tableView reloadData];
        }else if([responseObject count] == 0){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSError *error) {
        
    }showHub:YES];
}


- (UIButton *)set_rightButton{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 28, 20.5)];
    [button setTitle:@"新增" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = AdaptedFontSize(12);
    return button;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.tableView showBlankPageViewWithCount:self.SupecoalAUpervisorList reloadAction:@selector(loadDataRefresh) WithTarget:self];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentTableViewCell *cell = [CommentTableViewCell cellWithTableView:self.tableView];
    [cell settingWithSupecailModel:self.SupecoalAUpervisorList[indexPath.row]];
    cell.selectionStyle = UIAccessibilityTraitNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 83;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        QuotedSupervisorGC * model = self.SupecoalAUpervisorList[indexPath.row];
        SupecialSupervisorViewController * SupecialSupervisor = [[SupecialSupervisorViewController alloc]init];
        if ([model.state isEqualToString:@"2"]) {
            SupecialSupervisor.supecialSVGCState = SupecialSVGCDetailEdit;
        }else{
            SupecialSupervisor.supecialSVGCState = SupecialSVGCDetail;
        }
        SupecialSupervisor.indexpath = indexPath;
        SupecialSupervisor.LzyReloadData = ^(NSIndexPath *reload) {
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        };
        SupecialSupervisor.SupecialSVId = [NSString stringWithFormat:@"%d",model.id];
        [self.navigationController pushViewController:SupecialSupervisor animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    QuotedSupervisorGC *model = self.SupecoalAUpervisorList[indexPath.row];
    if ([model.state isEqualToString:@"2"]) {
        return YES;
    }else{
        return NO;
    }
}

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuotedSupervisorGC *model = self.SupecoalAUpervisorList[indexPath.row];
    UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
            [LZYNetworkHelper POST:dudaoresultdelete parameters:@{@"id":@(model.id)} success:^(id responseObject) {
                [LZYShowMsg showText:@"删除成功"];
                [self.SupecoalAUpervisorList removeObject:model];
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            } failure:^(NSError *error) {
                [LZYShowMsg showText:@"删除失败"];
            } showHub:YES];
        }];
    return @[deleteRoWAction];
}


- (void)right_button_event:(UIButton *)sender{
    LZYWeakSelf(self)
    SupecialSupervisorViewController * SupecialSupervisor = [[SupecialSupervisorViewController alloc]init];
    SupecialSupervisor.supecialSVGCState = SupecialSVGCAdd;
    SupecialSupervisor.LzyReloadData = ^(NSIndexPath *indexpath) {
        [weakself.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:SupecialSupervisor animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
